package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Goods;
import bean.User;
import dao.GoodsViewUser;
import dao.SelectOneGoods;
import dao.SoldOutMethod;

/**
 * Servlet implementation class SoldOutServlet
 * 将商品标识成没货状态，即将state改为0
 */
public class SoldOutServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SoldOutServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int id = Integer.parseInt(request.getParameter("id"));//获取通过url传来的id值
		if(SoldOutMethod.soldOut(id)){//将此id的物品state修改，若成功
			User user;
			Goods goods;
			
			goods = SelectOneGoods.getOneGoods(id);//获取这个物品信息
			
			int userid = goods.getUserId();//获取物品出售者id
			user = GoodsViewUser.getGoodsUser(userid);//得到这个出售者
			request.setAttribute("user", user);
			request.setAttribute("goods", goods);
			request.getRequestDispatcher("/view/detail.jsp").forward(request,//塞入request，转入展示页面
					response);
			return;
		}
		request.getRequestDispatcher("/fail.jsp").forward(request,//若失败，跳转失败界面
				response);
		return;
	}

}
